<?php
namespace Backend\Controllers\Setting\Sys;

use Backend\Controllers\BaseController;
use Backend\Models\Sys\Menu;

class MenuController extends BaseController
{

    public function initialize()
    {
    }

    public function getsAction()
    {
        $menu   = new Menu();
        $result = $menu->gets();
        if ($result === false) {
            return $this->json([
                'code' => 500,
                'msg'  => '获取失败，系统错误~'
            ]);
        }
        foreach ($result as $index => $item) {
            $result[$index]['level_name'] = $item['level'] == 1 ? '一级菜单' : ($item['level'] == 2 ? '二级菜单' : '三级菜单');
        }
        return $this->json([
            'code'  => 200,
            'menus' => $result
        ]);
    }

    public function getAction()
    {
        $id     = $this->request->getPost('id');
        $menu   = new Menu();
        $result = $menu->get($id);
        if ($result !== false) {
            $result['top'] = $result['top'] !== 0 ? $result['top'] : '';
            return $this->json([
                'code' => 200,
                'data' => $result
            ]);
        }
        return $this->json([
            'code' => 500,
            'msg'  => '获取失败，系统错误~'
        ]);
    }

    public function addAction()
    {
        $data = [
            'level'  => trim($this->request->getPost('level')),
            'name'   => trim($this->request->getPost('name')),
            'url'    => trim($this->request->getPost('url')),
            'status' => $this->request->getPost('status'),
            'is_nav' => $this->request->getPost('is_nav'),
            'top'    => $this->request->getPost('top'),
            'up_id'  => $this->request->getPost('up_id'),
            'icon'   => trim($this->request->getPost('icon')),
            'weight' => intval($this->request->getPost('weight')),
        ];
        if ($data['level'] == '') {
            return $this->json([
                'code' => 300,
                'msg'  => '请选择菜单等级~'
            ]);
        }
        if ($data['level'] == 2) {
            $data['up_id'] = $data['top'];
        }
        if ($data['up_id'] == '') {
            $data['up_id'] = 0;
        }
        if ($data['top'] == '') {
            $data['top'] = 0;
        }
        if ($data['name'] == '') {
            return $this->json([
                'code' => 301,
                'msg'  => '菜单名称不能为空~'
            ]);
        }
        if ($data['url'] == '') {
            return $this->json([
                'code' => 302,
                'msg'  => '菜单url不能为空~'
            ]);
        }
        if ($data['status'] == '') {
            return $this->json([
                'code' => 303,
                'msg'  => '请选择菜单状态~'
            ]);
        }
        if ($data['is_nav'] == '') {
            return $this->json([
                'code' => 304,
                'msg'  => '请选择显示状态~'
            ]);
        }
        if ($data['weight'] == '' || !is_numeric($data['weight'])) {
            return $this->json([
                'code' => 305,
                'msg'  => '菜单权重值不能为空~'
            ]);
        }
        $menu = new Menu();
        $data = $menu->add($data);
        if ($data) {
            return $this->json([
                'code' => 200,
                'msg'  => '新增菜单成功~'
            ]);
        } else {
            return $this->json([
                'code' => 201,
                'msg'  => '新增菜单失败，系统错误~'
            ]);
        }
    }

    public function editAction()
    {
        $data = [
            'id'     => trim($this->request->getPost('id')),
            'level'  => trim($this->request->getPost('level')),
            'name'   => trim($this->request->getPost('name')),
            'url'    => trim($this->request->getPost('url')),
            'status' => $this->request->getPost('status'),
            'is_nav' => $this->request->getPost('is_nav'),
            'top'    => $this->request->getPost('top'),
            'up_id'  => $this->request->getPost('up_id'),
            'icon'   => trim($this->request->getPost('icon')),
            'weight' => intval($this->request->getPost('weight')),
        ];
        if ($data['id'] == '') {
            return $this->json([
                'code' => 300,
                'msg'  => '系统错误~'
            ]);
        }
        if ($data['level'] == '') {
            return $this->json([
                'code' => 300,
                'msg'  => '请选择菜单等级~'
            ]);
        }
        if ($data['level'] === 2) {
            $data['up_id'] = $data['top'];
        }
        if ($data['up_id'] == '') {
            $data['up_id'] = 0;
        }
        if ($data['top'] == '') {
            $data['top'] = 0;
        }
        if ($data['name'] == '') {
            return $this->json([
                'code' => 301,
                'msg'  => '菜单名称不能为空~'
            ]);
        }
        if ($data['url'] == '') {
            return $this->json([
                'code' => 302,
                'msg'  => '菜单url不能为空~'
            ]);
        }
        if ($data['status'] == '') {
            return $this->json([
                'code' => 303,
                'msg'  => '请选择菜单状态~'
            ]);
        }
        if ($data['is_nav'] == '') {
            return $this->json([
                'code' => 304,
                'msg'  => '请选择显示状态~'
            ]);
        }
        if ($data['weight'] == '' || !is_numeric($data['weight'])) {
            return $this->json([
                'code' => 305,
                'msg'  => '菜单权重值不能为空~'
            ]);
        }

        $menu = new Menu();
        $data = $menu->edit($data);
        if ($data) {
            return $this->json([
                'code' => 200,
                'msg'  => '编辑菜单成功~'
            ]);
        } else {
            return $this->json([
                'code' => 201,
                'msg'  => '编辑菜单失败，系统错误~'
            ]);
        }
    }

    public function topMenusAction()
    {
        $menu   = new Menu();
        $result = $menu->tops();
        if ($result !== false) {
            return $this->json([
                'code' => 200,
                'data' => $result
            ]);
        }
        return $this->json([
            'code' => 500,
            'msg'  => '获取失败，系统错误~'
        ]);
    }

    public function upMenusAction()
    {
        $level = $this->request->getPost('level');
        $top   = $this->request->getPost('top');
        if ($level == '') {
            return $this->json([
                'code' => 300,
                'msg'  => '获取上级菜单失败，请选择菜单等级~'
            ]);
        } else {
            $level = $level - 1;
        }
        if ($top == '') {
            return $this->json([
                'code' => 301,
                'msg'  => '获取上级菜单失败，请选择顶级菜单~'
            ]);
        }
        $menu   = new Menu();
        $result = $menu->ups($level, $top);
        if ($result !== false) {
            return $this->json([
                'code' => 200,
                'data' => $result
            ]);
        }
        return $this->json([
            'code' => 500,
            'msg'  => '获取失败，系统错误~'
        ]);
    }
}
